package com.wing.product.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.db.config.util.DynamicDataSource;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.ProductCategory;
import com.wing.product.model.response.ProductCategoryVO;
import com.wing.product.service.ProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.product.model.request.ProductCategoryForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-产品分类管理", tags = "产品分类管理")
@RestController("appProductCategoryController")
@RequestMapping("product/app/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private LocaleMessage localeMessage;
    @ApiOperation(value = "查询所有产品分类", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductCategoryVO> list(
            @RequestParam(name = "page" ,defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "asc") String order
            ,ProductCategory.Type type
    ) {

        Page<ProductCategoryVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if (type!=null){
            params.put("type",type.getValue());
        }else{
            params.put("type",ProductCategory.Type.knife.getValue());
        }

        params.put("dataSourceType",ProductCategory.DataSourceType.enterprise.getValue());
        Page<ProductCategoryVO> list = productCategoryService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "查询所有产品分类", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/getAllList")
    public JsonResult<List<ProductCategoryVO>> getAllList(
            @RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "asc") String order
            ,ProductCategory.Type type
            ,ProductCategory.CategoryType categoryType
    ) {
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if (type!=null){
            params.put("type",type.getValue());
        }else{
            params.put("type",ProductCategory.Type.knife.getValue());
        }
        if(categoryType != null){
            params.put("categoryType", categoryType.getValue());
        }else{
            params.put("categoryType", ProductCategory.CategoryType.purchase.getValue());
        }
        params.put("parentId",-1);
        params.put("dataSourceType",ProductCategory.DataSourceType.enterprise.getValue());
        List<ProductCategoryVO> list = productCategoryService.list4app(params);
        for(ProductCategoryVO item : list){
            Map<String, Object> sonParams = new HashMap<>();
            sonParams.put("parentId",item.getId());
            List<ProductCategoryVO> categoryVOList = productCategoryService.list4app(sonParams);
            item.setSonProductCategoryList(categoryVOList);
        }
        return JsonResult.success(list);
    }

    @GetMapping("/detail/{id}")
    public JsonResult detail(@PathVariable("id") Long productCategoryId) {

        ProductCategoryVO productCategoryVO = productCategoryService.get4appById(productCategoryId);
        if (productCategoryVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), productCategoryVO);
    }

    @PostMapping()
    public JsonResult add(@Validated ProductCategoryForm productCategoryForm) {

        if (productCategoryService.add(productCategoryForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
